<?php
/**
* @file $Id: PrepareDate.fnc.php 396 2007-01-22 20:58:53Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

// SEND PrepareDate a name prefix, and a date in oracle format 'd-M-y' as the selected date to have returned a date selection series
// of pull-down menus
// For the default to be Not Specified, send a date of 00-000-00 -- For today's date, send nothing
// The date pull-downs will create three variables, monthtitle, daytitle, yeartitle
// The third parameter (booleen) specifies whether Not Specified should be allowed as an option

function PrepareDate($date='',$title='',$allow_na=true,$options='')
{	global $_FOCUS;

	if($options=='')
		$options = array();
	if(!$options['Y'] && !$options['M'] && !$options['D'] && !$options['C'])
		$options += array('Y'=>true,'M'=>true,'D'=>true,'C'=>true);
		
	if($options['short']==true)
		$extraM = "style='width:60;' ";
	if($options['submit']==true)
	{
		$tmp_REQUEST['M'] = $tmp_REQUEST['D'] = $tmp_REQUEST['Y'] = $_REQUEST;
		unset($tmp_REQUEST['M']['month'.$title]);
		unset($tmp_REQUEST['D']['day'.$title]);
		unset($tmp_REQUEST['Y']['year'.$title]);
		$extraM .= "onchange='document.location.href=\"".PreparePHP_SELF($tmp_REQUEST['M'])."&amp;month$title=\"+this.form.month$title.value;'";
		$extraD .= "onchange='document.location.href=\"".PreparePHP_SELF($tmp_REQUEST['D'])."&amp;day$title=\"+this.form.day$title.value;'";
		$extraY .= "onchange='document.location.href=\"".PreparePHP_SELF($tmp_REQUEST['Y'])."&amp;year$title=\"+this.form.year$title.value;'";
	}
	
	if($options['C'])
		$_FOCUS['PrepareDate']++;

	if(strlen($date)==9) // ORACLE
	{
		$day = substr($date,0,2);
		$month = substr($date,3,3);
		$year = substr($date,7,2);

		$return .= '<!-- '.$year.MonthNWSwitch($month,'tonum').$day.' -->';
	}
	else // POSTGRES
	{
		$day = substr($date,8,2);
		$month = MonthNWSwitch(substr($date,5,2),'tochar');
		$year = substr($date,2,2);

		$return .= '<!-- '.$year.MonthNWSwitch($month,'tonum').$day.' -->';
	}

	// MONTH  ---------------
	if($options['M'])
	{
		$return .= "<SELECT NAME=month".$title." id=monthSelect".$_FOCUS['PrepareDate']." SIZE=1 $extraM>";
		if($allow_na)
		{
			if($month=='000')
				$return .= "<OPTION value=\"\" SELECTED>"._('N/A');else $return .= "<OPTION value=\"\">"._('N/A');
		}
		
		if($month=='JAN'){$return .= "<OPTION VALUE=JAN SELECTED>"._('January');}else{$return .= "<OPTION VALUE=JAN>"._('January');}
		if($month=='FEB'){$return .= "<OPTION VALUE=FEB SELECTED>"._('February');}else{$return .= "<OPTION VALUE=FEB>"._('February');}
		if($month=='MAR'){$return .= "<OPTION VALUE=MAR SELECTED>"._('March');}else{$return .= "<OPTION VALUE=MAR>"._('March');}
		if($month=='APR'){$return .= "<OPTION VALUE=APR SELECTED>"._('April');}else{$return .= "<OPTION VALUE=APR>"._('April');}
		if($month=='MAY'){$return .= "<OPTION VALUE=MAY SELECTED>"._('May');}else{$return .= "<OPTION VALUE=MAY>"._('May');}
		if($month=='JUN'){$return .= "<OPTION VALUE=JUN SELECTED>"._('June');}else{$return .= "<OPTION VALUE=JUN>"._('June');}
		if($month=='JUL'){$return .= "<OPTION VALUE=JUL SELECTED>"._('July');}else{$return .= "<OPTION VALUE=JUL>"._('July');}
		if($month=='AUG'){$return .= "<OPTION VALUE=AUG SELECTED>"._('August');}else{$return .= "<OPTION VALUE=AUG>"._('August');}
		if($month=='SEP'){$return .= "<OPTION VALUE=SEP SELECTED>"._('September');}else{$return .= "<OPTION VALUE=SEP>"._('September');}
		if($month=='OCT'){$return .= "<OPTION VALUE=OCT SELECTED>"._('October');}else{$return .= "<OPTION VALUE=OCT>"._('October');}
		if($month=='NOV'){$return .= "<OPTION VALUE=NOV SELECTED>"._('November');}else{$return .= "<OPTION VALUE=NOV>"._('November');}
		if($month=='DEC'){$return .= "<OPTION VALUE=DEC SELECTED>"._('December');}else{$return .= "<OPTION VALUE=DEC>"._('December');}
		$return .= "</SELECT>";
	}

	// DAY  ---------------
	if($options['D'])
	{
		$return .="<SELECT NAME=day".$title." id=daySelect".$_FOCUS['PrepareDate']." SIZE=1 $extraD>";
		if($allow_na)
		{
			if($day=='00'){$return .= "<OPTION value=\"\" SELECTED>"._('N/A');}else{$return .= "<OPTION value=\"\">"._('N/A');}
		}
		
		for($i=1;$i<=31;$i++)
		{
			if(strlen($i)==1)
				$print='0'.$i;
			else
				$print = $i;
			
			$return .="<OPTION VALUE=".$print;
			if($day==$print)
				$return .=" SELECTED";
			$return .=">$i ";
		}
		$return .="</SELECT>";
	}
	
	// YEAR	 ---------------
	if($options['Y'])
	{
		if(!$year || $options['big_year_range'])
		{
			$begin = date('Y') - 80;
			$end = date('Y') + 5;
			if($year && $options['big_year_range'])
			{
				if($year<50)
					$year = '20'.$year;
				else
					$year = '19'.$year;
			}
		}
		else
		{
			if($year<50)
				$year = '20'.$year;
			else
				$year = '19'.$year;
			$begin = $year - 5;
			$end = $year + 5;
		}
	
		$return .="<SELECT NAME=year".$title." id=yearSelect".$_FOCUS['PrepareDate']." SIZE=1 $extraY>";
		if($allow_na)
		{
			if($year=='00'){$return .= "<OPTION value=\"\" SELECTED>"._('N/A');}else{$return .= "<OPTION value=\"\">"._('N/A');}
		}
			
		for($i=$end;$i>=$begin;$i--)
		{
			$return .="<OPTION VALUE=".substr($i,2);
			if($year==$i){$return .=" SELECTED";}
			$return .=">".$i;
		}
		$return .="</SELECT>";
	}
	
	if($options['C'])
		$return .= '<img src="assets/jscalendar/img.gif" id="trigger'.$_FOCUS['PrepareDate'].'" style="cursor: pointer; border: 1px solid red;" onmouseover=this.style.background="red"; onmouseout=this.style.background=""; />';
	
	if($_REQUEST['_FOCUS_PDF'])
		$return = ProperDate($date);
	return $return;
}

?>